<?php

namespace addon\lian\shop\controller;

use addon\lian\model\Config as ConfigModel;
use addon\lian\model\sqlmodel\LianLevelModel;
use app\shop\controller\BaseShop;
use addon\lian\model\LianLevel;

/**
 *  链动等级管理
 */
class Level extends BaseShop
{

    /**
     * 链动等级列表
     */
    public function lists()
    {
        $model = new LianLevelModel();
        $field = '*';
        if (request()->isJson()) {

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $list = $model->getPage([ [ 'site_id', '=', $this->site_id ] ], $page, $page_size, 'level_num asc', $field);

            return $list;

        } else {

            //获取系统配置
            $config_model = new ConfigModel();
            $basics = $config_model->getLianBasicsConfig($this->site_id);
            $this->assign("basics_info", $basics[ 'data' ][ 'value' ]);
            return $this->fetch('level/lists');
        }

    }

    /**
     * 添加链动等级
     */
    public function add()
    {
        $model = new LianLevel();

        if (request()->isJson()) {

            $data = [
                'site_id' => $this->site_id,
                'level_name' => input('level_name', ''),
                'level_num' => input('level_num', 1),
                'recommend_rate' => input('recommend_rate', 0),
                'recommend_money' => input('recommend_money', 0),
                'see_rate' => input('see_rate', 0),
                'see_money' => input('see_money', 0),
                'equal_rate' => input('equal_rate', 0),
                'equal_money' => input('equal_money', 0),
                'cultivate_rate' => input('cultivate_rate', 0),
                'cultivate_money' => input('cultivate_money', 0),
                'upgrade_type' => input('upgrade_type', 0),
                'order_money' => input('order_money', 0),
                'recharge_money' => input('recharge_money', 0),
                'recommend_num' => input('recommend_num', 0),
                'up_goods_id' => input('up_goods_id', ''),
                'up_goods_num' => input('up_goods_num', 0),
                'up_register' => input('up_register', 0),
                'see_frozen' => input('see_frozen', 0),
                'cultivate_frozen' => input('cultivate_frozen', 0),
                'frozen_rate' => input('frozen_rate', 0),
                'task_boss_num' => input('task_boss_num', 0),
                'task_boss_first' => input('task_boss_first', 1),
                'task_limit_days' => input('task_limit_days', 0),
                'fusee_rate' => input('fusee_rate', 0),
                'fusee_money' => input('fusee_money', 0),
                'fusee_frozen' => input('fusee_frozen', 0),
            ];
            $res = $model->addLevel($data);
            return $res;
        } else {
            //获取系统配置
            $config_model = new ConfigModel();
            $basics = $config_model->getLianBasicsConfig($this->site_id);
            $this->assign("basics_info", $basics[ 'data' ][ 'value' ]);

            $level_weight = $model->getLevelList([ [ 'level_num', '<>', '' ], [ 'site_id', '=', $this->site_id ] ], 'level_num');
            $level_weight = $level_weight[ 'data' ];
            if (!empty($level_weight)) $level_weight = array_column($level_weight, 'level_num');
            $this->assign('level_weight', $level_weight);

            return $this->fetch('level/add');
        }

    }

    /**
     * 编辑链动等级
     */
    public function edit()
    {
        $model = new LianLevel();

        if (request()->isJson()) {

            $data = [
                'level_name' => input('level_name', ''),
                'level_num' => input('level_num', 0),
                'recommend_rate' => input('recommend_rate', 0),
                'recommend_money' => input('recommend_money', 0),
                'see_rate' => input('see_rate', 0),
                'see_money' => input('see_money', 0),
                'equal_rate' => input('equal_rate', 0),
                'equal_money' => input('equal_money', 0),
                'cultivate_rate' => input('cultivate_rate', 0),
                'cultivate_money' => input('cultivate_money', 0),
                'upgrade_type' => input('upgrade_type', 0),
                'order_money' => input('order_money', 0),
                'recharge_money' => input('recharge_money', 0),
                'recommend_num' => input('recommend_num', 0),
                'up_goods_id' => input('up_goods_id', ''),
                'up_goods_num' => input('up_goods_num', 0),
                'up_register' => input('up_register', 0),
                'see_frozen' => input('see_frozen', 0),
                'cultivate_frozen' => input('cultivate_frozen', 0),
                'frozen_rate' => input('frozen_rate', 0),
                'task_boss_num' => input('task_boss_num', 0),
                'task_boss_first' => input('task_boss_first', 1),
                'task_limit_days' => input('task_limit_days', 0),
                'fusee_rate' => input('fusee_rate', 0),
                'fusee_money' => input('fusee_money', 0),
                'fusee_frozen' => input('fusee_frozen', 0),
            ];
            $level_id = input('level_id', '');

            $res = $model->editLevel($data, [ [ 'level_id', '=', $level_id ], [ 'site_id', '=', $this->site_id ] ]);
            return $res;
        } else {
            $level_id = input('level_id', '');
            $info = $model->getLevelInfo([ [ 'level_id', '=', $level_id ], [ 'site_id', '=', $this->site_id ] ]);
            if (empty($info[ 'data' ])) $this->error('未获取到等级数据', href_url('lian://shop/level/lists'));
            $this->assign('info', $info[ 'data' ]);

            //获取系统配置
            $config_model = new ConfigModel();
            $basics = $config_model->getLianBasicsConfig($this->site_id);
            $this->assign("basics_info", $basics[ 'data' ][ 'value' ]);

            $level_weight = $model->getLevelList([ [ 'level_num', '<>', '' ], [ 'level_id', '<>', $level_id ], [ 'site_id', '=', $this->site_id ] ], 'level_num');
            $level_weight = $level_weight[ 'data' ];
            if (!empty($level_weight)) $level_weight = array_column($level_weight, 'level_num');
            $this->assign('level_weight', $level_weight);
        }

        return $this->fetch('level/edit');
    }

    /**
     * 删除链动等级
     */
    public function delete()
    {
        $model = new LianLevel();

        $level_id = input('level_id', '');
        $res = $model->deleteLevel($level_id, $this->site_id);
        return $res;
    }
}